﻿package com.game.view.component.notification
{
	import com.dusk.util.FilterUtil;
	import com.dusk.util.TextUtil;
	import com.game.view.component.ComponentBase;
	import com.greensock.TweenMax;
	
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.Timer;
	
	public class AlertBox extends ComponentBase
	{
		
		public function AlertBox()
		{
			super();
		}
		
		private var _tf:TextFormat;
		private var _txt:TextField;
		private var _timer:Timer;
		private var _time:Number = 1;
		
		override protected function beforeAdded():void
		{
			this.x = 0;
			this.y = 270;
			_txt = TextUtil.creatTextField("",TextUtil.UNIVERSAL_FONT,30,TextFormatAlign.CENTER);
			FilterUtil.setGlow(_txt,0xFFFFFF, 1, 2, 2, 5.3, 3);
			FilterUtil.clearCache();
			_txt.x = _txt.y = 0;
			_timer = new Timer(1000);
			_timer.addEventListener(TimerEvent.TIMER, onCount);
		}
		
		/**
		 * 设置显示文字
		 * @param str 显示内容
		 * @param time 文字持续时间，默认1s
		 * @param fontSize 文字大小，默认30
		 */
		public function setTxt(str:String, time:int = 1, fontSize:int = 30):void
		{
			if (str.length > 25)
			{
				time++;
			}
			this._time = time;
			this._tf = TextUtil.getDefaultTextFormat();
			this._tf.size = fontSize;
			_txt.text = str;
			_txt.setTextFormat(_tf);
			this.alpha = 1;
			this.y = 270;
			this._timer.stop();
			this._timer.reset();
			this._timer.start();
			if (!this.contains(_txt))
			{
				_txt.height = 40;
				_txt.width = 940;
				addChild(_txt);
			}
			TweenMax.killTweensOf(this);
		}
		
		override protected function beforeRemoved():void
		{
			this.alpha = 1;
			this.y = 270;
		}
		
		private function onCount(evt:TimerEvent):void
		{
			if (_timer.currentCount == this._time + 1)
			{
				this.removeFromParent();
				this._timer.stop();
				this._timer.reset();
			}
			else if (_timer.currentCount == this._time)
			{
				TweenMax.to(this, 0.5, {
					"y"    : 240,
					"alpha": 0
				});
			}
		}
	}
}
